package com.xhh.nphm.web.controller.system;

import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.common.utils.DateUtils;
import com.xhh.nphm.common.utils.ShiroUtils;
import com.xhh.nphm.system.domain.TtelGetDealEty;
import com.xhh.nphm.system.domain.TwfSendBackEty;
import com.xhh.nphm.system.domain.TworkFormBaseEty;
import com.xhh.nphm.system.mapper.WfSendBackDao;
import com.xhh.nphm.system.service.ITelGetDealService;
import com.xhh.nphm.system.service.ITworkFormLogService;
import com.xhh.nphm.system.service.IWfSendBackService;
import com.xhh.nphm.system.service.IWorkFormBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 首页实时满意情况报表控制层
 */
@RestController
@RequestMapping("/system/report/tel")
public class ReportTelController extends BaseController {
    private final String prefix = "system/report/tel";

    /**
     * 电话拟派工单服务层接口
     */
    @Autowired
    private IWfSendBackService wfSendBackService;
    /**
     * 电话工单服务层接口
     */
    @Autowired
    private IWorkFormBaseService workFormBaseService;
    /**
     * 通话记录服务层接口
     */
    @Autowired
    private ITelGetDealService telGetDealService;

    /**
     * 进入办理量列表页面
     */
    @GetMapping("/reportOver")
    public ModelAndView reportOver() {
        return new ModelAndView(prefix + "/reportOver");
    }

    /**
     * 加载办理量列表数据
     * @param sendBackEty 封装查询条件
     */
    @PostMapping("/reportOverListJson")
    public TableDataInfo reportOverListJson(TwfSendBackEty sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",String.valueOf(getSysUser().getDeptId()));
        params.put("startTime",getStartDate());
        params.put("endTime",getEndDate());
        List<Map<String, Object>> list = wfSendBackService.rptReplyOverListJson(params);
        return getDataTable(list);
    }

    /**
     * 进入未转派列表页面
     */
    @GetMapping("/sendWait")
    public ModelAndView sendWait() {
        return new ModelAndView(prefix + "/sendWait");
    }

    /**
     * 加载未转派列表数据
     * @param sendBackEty 封装查询条件
     */
    @PostMapping("/sendWaitListJson")
    public TableDataInfo sendWaitListJson(TwfSendBackEty sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list;
        if(IS_GAOXIN()){
            list = wfSendBackService.rptSendWaitListJson_gxq(params);
        }else{
            list = wfSendBackService.rptSendWaitListJson(params);
        }
        return getDataTable(list);
    }

    /**
     * 进入被市级拒退工单页面
     */
    @GetMapping("/notPass")
    public ModelAndView notPass() {
        return new ModelAndView(prefix + "/notPass");
    }

    /**
     * 加载被市级拒退工单列表数据
     * @param sendBackEty 封装查询条件
     */
    @PostMapping("/notPassListJson")
    public TableDataInfo notPassListJson(TwfSendBackEty sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list = wfSendBackService.rptNotPassListJson(params);
        return getDataTable(list);
    }


    /**
     * 进入临期工单列表页面
     *
     * @return 视图
     */
    @GetMapping("/muBlueS")
    public ModelAndView muBlueS() {
        return new ModelAndView(prefix + "/muBlueS");
    }


    /**
     * 加载临期工单列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/muBlueSJson")
    public TableDataInfo muBlueSJson(TwfSendBackEty sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",String.valueOf(getSysUser().getDeptId()));
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list = wfSendBackService.muBlueSJson(params);
        return getDataTable(list);
    }

    /**
     * 进入超期工单列表页面
     *
     * @return 视图
     */
    @GetMapping("/muRedS")
    public ModelAndView muRedS() {
        return new ModelAndView(prefix + "/muRedS");
    }


    /**
     * 加载超期工单列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/muRedSJson")
    public TableDataInfo muRedSJson(TwfSendBackEty sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",String.valueOf(getSysUser().getDeptId()));
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list = wfSendBackService.muRedSJson(params);
        return getDataTable(list);
    }

    /**
     * 进入未签收工单列表页面
     *
     * @return 视图
     */
    @GetMapping("/muSignNot")
    public ModelAndView muSignNot() {
        return new ModelAndView(prefix + "/muSignNot");
    }


    /**
     * 加载未签收工单列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/muSignNotJson")
    public TableDataInfo muSignNotJson(TwfSendBackEty sendBackEty) {
        startPage();
        List<Map<String, Object>> list;
        if (IS_GAOXIN()){
            list = wfSendBackService.rptSignNotJson_gxq(getSysUser().getDeptId().toString(),sendBackEty);
        }else{
            list = wfSendBackService.rptSignNotJson(getSysUser().getDeptId().toString(),sendBackEty);
        }
        return getDataTable(list);
    }

    /**
     * 进入已退工单列表页面
     *
     * @return 视图
     */
    @GetMapping("/muDeptBack")
    public ModelAndView muDeptBack() {
        return new ModelAndView(prefix + "/muDeptBack");
    }


    /**
     * 加载已退工单列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/muDeptBackJson")
    public TableDataInfo muDeptBackJson(TwfSendBackEty sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sbdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list = wfSendBackService.rptDeptBackJson(params);
        return getDataTable(list);
    }

    /**
     * 进入承办单位未签收T列表页面
     * @return 视图
     */
    @GetMapping("/listSonSignNot")
    public ModelAndView listSonSignNot() {
        return new ModelAndView(prefix + "/listSonSignNot");
    }

    /**
     * 加载承办单位未签收T列表页面

     * @return 列表数据
     */
    @PostMapping("/listSonSignNotJson")
    public TableDataInfo listSonSignNotJson(TwfSendBackEty sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list;
        if (IS_GAOXIN()){
            list = wfSendBackService.listSonSignNotJson_gxq(params);
        }else{
            list = wfSendBackService.listSonSignNotJson(params);
        }
        return getDataTable(list);
    }
    
    /**
     * 进入承办单位已退工单T列表页面
     */
    @GetMapping("/listSonAgreeBack")
    public ModelAndView listSonAgreeBack() {
        return new ModelAndView(prefix + "/listSonAgreeBack");
    }

    /**
     * 加载承办单位已退T列表数据
     */
    @PostMapping("/listSonAgreeBackJson")
    public TableDataInfo listSonAgreeBackJson(TwfSendBackEty sendBackEty) {
        startPage();
        //封装查询条件
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("sendBack",sendBackEty);
        params.put("sdeptId",getSysUser().getDeptId().toString());
//        params.put("startTime",getStartDate());
//        params.put("endTime",getEndDate());
        List<Map<String, Object>> list;
        if (IS_GAOXIN()){
            list = wfSendBackService.listSonAgreeBackJson_gxq(params);
        }else{
            list = wfSendBackService.listSonAgreeBackJson(params);
        }
        return getDataTable(list);
    }

    /**
     * 查看电话工单详情
     * @param wfId 工单编号
     * @param map 模型
     * @return 视图
     */
    @GetMapping("/caseDetail/{wfId}")
    public ModelAndView caseDetail(@PathVariable("wfId") Integer wfId, ModelMap map) {
        TworkFormBaseEty tworkFormBaseEty = workFormBaseService.getById(wfId);
        TtelGetDealEty ttelGetDealEty = telGetDealService.getById(tworkFormBaseEty.getGetDealPk());
        map.put("workFormBase", tworkFormBaseEty);
        map.put("telGetDeal", ttelGetDealEty);
        return new ModelAndView(prefix + "/caseDetail");
    }

    /**
     * 查询下级部门的电话未签收、未回复、退回和超期情况数据统计
     *  2023/1/3
     *  @Param params
     */
    @PostMapping("/listTelSubStatisticsJson")
    public List<Map<String, Object>> listTelSubStatisticsJson(ModelMap modelMap) {
        HashMap<String,Object> params = new HashMap<>(4);
        params.put("deptId", getSysUser().getDeptId());
        params.put("startTime", getStartDate());
        params.put("endTime", getEndDate());
        params.put("flag", IS_GAOXIN());
        List<Map<String, Object>> listTelSubStatistics = wfSendBackService.listTelSubStatisticsJson(params);
        modelMap.put("listTelSubStatistics",listTelSubStatistics);
        return listTelSubStatistics;
    }

    /**
     * 下级部门电话工单详情列表
     * @param subDeptId 下级部门
     * @return 视图
     */
    @GetMapping("/subDeptTelDetail/{subDeptId}")
    public ModelAndView subDeptTelDetail(@PathVariable("subDeptId") String subDeptId, ModelMap map) {
        map.put("subDeptId",subDeptId);
        return new ModelAndView(prefix + "/subDeptTelDetail");
    }

    /**
     * 下级部门电话工单详情列表
     * @param subDeptId 下级部门
     * @return 视图
     */
    @PostMapping("/subDeptTelDetailJson/{subDeptId}")
    public TableDataInfo subDeptTelDetailJson(@PathVariable("subDeptId") String subDeptId,TwfSendBackEty twfSendBackEty) {
        startPage();
        List<Map<String,Object>> list;
        if(IS_GAOXIN()){
            list = wfSendBackService.subDeptTelDetailJson_gxq(subDeptId,twfSendBackEty);
        }  else{
            list = wfSendBackService.subDeptTelDetailJson(subDeptId,twfSendBackEty);
        }
        return getDataTable(list);
    }

    /**
     * 获取起始时间
     * @return 起始时间
     */
    private Date getStartDate(){
        Calendar startDate = Calendar.getInstance();
        if (!isOverTwentyFive()) {
            startDate.set(Calendar.MONTH,startDate.get(Calendar.MONTH) - 1);
        }
        startDate.set(Calendar.DAY_OF_MONTH,25);
        startDate.set(Calendar.HOUR_OF_DAY,0);
        startDate.set(Calendar.MINUTE,0);
        startDate.set(Calendar.SECOND,0);
        return startDate.getTime();
    }

    /**
     * 获取结束时间
     * @return 结束时间
     */
    private Date getEndDate(){
        Calendar endDate = Calendar.getInstance();
        if (isOverTwentyFive()) {
            endDate.set(Calendar.MONTH,endDate.get(Calendar.MONTH) + 1);
        }
        endDate.set(Calendar.DAY_OF_MONTH,26);
        endDate.set(Calendar.HOUR_OF_DAY,0);
        endDate.set(Calendar.MINUTE,0);
        endDate.set(Calendar.SECOND,0);
        return endDate.getTime();
    }

    /**
     * 当前日期是否超出25号
     * @return 布尔
     */
    private boolean isOverTwentyFive(){
        Calendar curDate = Calendar.getInstance();
        boolean isOverTwentyFive = false;
        int day = curDate.get(Calendar.DAY_OF_MONTH);
        if (day >= 25) {
            isOverTwentyFive = true;
        }
        return isOverTwentyFive;
    }
}
